﻿using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Options;
using Microsoft.VisualBasic.FileIO;
using LaserMark.Components.Services;
using LaserMark.Control.Service;
using LaserMark.View.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;

namespace LaserMark.Components
{
    public partial class Speed
    {
        [Parameter]
        public double Value { get; set; }

        [Parameter]
        public EventCallback<double> ValueChanged { get; set; }

        private double Percent { get; set; }

        [Inject]
        [NotNull]
        private IOptions<MarkOption>? MarkOptions { get; set; }

        [Inject]
        [NotNull]
        private IControl? Control { get; set; }

        [Inject]
        [NotNull]
        private IGlobalStatus? GlobalStatus { get; set; }

        [NotNull]
        private MarkOption? MarkOption { get; set; }

        [NotNull]
        private CancellationTokenSource? Token { get; set; } = new();

        private bool IsAuto { get; set; }

        private bool IsManual { get; set; }

        [NotNull]
        private double Max { get; set; }

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            MarkOption = MarkOptions.Value;
            await Control.GetDi(0, mc.MC_GPI, out int value);
            var aa = Convert.ToString(value, 2).PadLeft(16, '0');
            if (aa[12] == '1')
            {
                Value = MarkOption.ManualVel.X / 1000 * 60;
                Max = MarkOption.ManualVel.X / 1000 * 60;
                Percent = Value / Max * 100;
                IsManual = true;
            }
            else
            {
                Value = MarkOption.MarkVel.X / 1000 * 60;
                Max = MarkOption.MarkVel.X / 1000 * 60;
                Percent = Value / Max * 100;
                IsAuto = true;
            }

            Timer timer = new(1000);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        private async void Timer_Elapsed(object? sender, ElapsedEventArgs e)
        {
            try
            {
                await Control.GetDi(0, mc.MC_GPI, out int value);
                var aa = Convert.ToString(value, 2).PadLeft(16, '0');
                if (aa[12] == '1' && !IsManual)
                {
                    IsManual = true;
                    IsAuto = false;
                    Value = MarkOption.ManualVel.X / 1000 * 60;
                    Max = MarkOption.ManualVel.X / 1000 * 60;
                    Percent = Value / Max * 100;
                    await InvokeAsync(StateHasChanged);
                }
                else if (aa[12] == '0' && !IsAuto)
                {
                    IsAuto = true;
                    IsManual = false;
                    Value = MarkOption.MarkVel.X / 1000 * 60;
                    Max = MarkOption.MarkVel.X / 1000 * 60;
                    Percent = Value / Max * 100;
                    await InvokeAsync(StateHasChanged);
                }
            }
            catch (Exception)
            {

            }
        }

        private Task OnValueChanged(double val)
        {
            Value = val;

            if (IsAuto)
            {
                Percent = val / Max * 100;
                if (GlobalStatus.IsMarked)
                {
                    mc.GT_SetOverride(0, 1, val / Max);
                }
                MarkOptions.Value.MarkVel.X = val * 1000 / 60;
            }

            if (IsManual)
            {
                Percent = val / Max * 100;
                MarkOptions.Value.ManualVel.X = val * 1000 / 60;
                MarkOptions.Value.ManualVel.Y = val * 1000 / 60;
            }
            StateHasChanged();

            return Task.CompletedTask;
        }
    }
}
